using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Emlyn.Applications.LochAlarm
{
    [XmlRoot("AlarmSettings")]
    public class AlarmSettings: IXmlSerializable
    {
        private bool _enabled;
        private Dictionary<DayOfWeek, DailySettings> _allDailySettings;
        private string _alarmSoundFileName;
        private string _alarmPlayListName = String.Empty;
        private bool _autoRun;
        private int _alarmLengthMins = 0;

        public AlarmSettings()
        {
            _allDailySettings = new Dictionary<DayOfWeek, DailySettings>();
        }

        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        public string AlarmSoundFileName
        {
            get { return _alarmSoundFileName; }
            set { _alarmSoundFileName = value; }
        }

        public string AlarmPlayListName
        {
            get { return _alarmPlayListName; }
            set { _alarmPlayListName = value; }
        }

        public int AlarmLengthMins
        {
            get { return _alarmLengthMins; }
            set { _alarmLengthMins = value; }
        }

        public bool AutoRun
        {
            get { return _autoRun; }
            set { _autoRun = value; }
        }


        public DailySettings GetDailySettings(DayOfWeek aDay)
        {
            DailySettings lretval = null;

            if (!_allDailySettings.ContainsKey(aDay))
                _allDailySettings.Add(aDay, new DailySettings());
            
            lretval = _allDailySettings[aDay];

            return lretval;
        }

        public void SetDailySettings(DayOfWeek aDay, DailySettings aSettings)
        {
            if (aSettings != null)
            {
                _allDailySettings[aDay] = aSettings;
            }
        }

        public DailySettings GetDailySettingsForToday()
        {
            return GetDailySettings(DateTime.Today.DayOfWeek);
        }
        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            reader.Read();

            while (reader.LocalName != "AlarmSettings")
            {
                if (reader.LocalName == "Enabled")
                {
                    reader.ReadStartElement();
                    _enabled = reader.ReadContentAsBoolean();
                    reader.ReadEndElement();
                }
                else if (reader.LocalName == "AlarmSoundFileName")
                {
                    if (!reader.IsEmptyElement)
                    {
                        reader.ReadStartElement();
                        _alarmSoundFileName = reader.ReadContentAsString();
                        reader.ReadEndElement();
                    }
                    else
                        reader.Read();
                }
                else if (reader.LocalName == "AlarmPlayListName")
                {
                    if (!reader.IsEmptyElement)
                    {
                        reader.ReadStartElement();
                        _alarmPlayListName = reader.ReadContentAsString();
                        reader.ReadEndElement();
                    }
                    else
                        reader.Read();
                }
                else if (reader.LocalName == "AlarmLengthMins")
                {
                    if (!reader.IsEmptyElement)
                    {
                        reader.ReadStartElement();
                        try
                        {
                            _alarmLengthMins = Convert.ToInt32(reader.ReadContentAsString());
                        }
                        catch { }
                        reader.ReadEndElement();
                    }
                    else
                        reader.Read();
                }
                else if (reader.LocalName == "Autorun")
                {
                    reader.ReadStartElement();
                    _autoRun = reader.ReadContentAsBoolean();
                    reader.ReadEndElement();
                }
                else
                {
                    DayOfWeek lday;
                        
                    try
                    {
                        lday = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), reader.LocalName);
                        reader.ReadStartElement();

                        XmlSerializer lserializer = new XmlSerializer(typeof(DailySettings));
                        DailySettings ldailySettings = lserializer.Deserialize(reader) as DailySettings;

                        _allDailySettings[lday] = ldailySettings;

                        reader.ReadEndElement();
                    }
                    catch
                    {
                        reader.Read();
                    }
                }
            }

            reader.Read();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("Enabled");
            writer.WriteValue(_enabled);
            writer.WriteEndElement();

            if (_alarmSoundFileName != null)
            {
                writer.WriteStartElement("AlarmSoundFileName");
                writer.WriteValue(_alarmSoundFileName);
                writer.WriteEndElement();
            }

            if (_alarmPlayListName != null)
            {
                writer.WriteStartElement("AlarmPlayListName");
                writer.WriteValue(_alarmPlayListName);
                writer.WriteEndElement();
            }

            if (_alarmLengthMins > 0)
            {
                writer.WriteStartElement("AlarmLengthMins");
                writer.WriteValue(_alarmLengthMins);
                writer.WriteEndElement();
            }

            writer.WriteStartElement("Autorun");
            writer.WriteValue(_autoRun);
            writer.WriteEndElement();

            foreach (DayOfWeek lday in Enum.GetValues(typeof(DayOfWeek)))
            {
                writer.WriteStartElement(lday.ToString());

                XmlSerializer lserializer = new XmlSerializer(typeof(DailySettings));
                lserializer.Serialize(writer, GetDailySettings(lday));

                writer.WriteEndElement();
            }
        }

        #endregion
    }
}
